<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('关联的指标')" prop="subKeyId">
            <span slot="label">
              {{ $t('关联的指标') }}
              <el-tooltip placement="top">
                <div slot="content">
                  <span v-html="$t('因为关联的指标不能带参数，所以这里是查询所有为「视图」的指标')" />
                </div>
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <!-- <el-input v-model="form.subKeyId" :placeholder="$t('选择关联的指标Id')" show-word-limit maxlength="32" /> -->
            <el-select
              v-model="form.subKeyId"
              clearable
              filterable
              :placeholder="$t('请选择')"
              style="width: 100%"
            >
              <el-option
                v-for="keys in paramsKeyList"
                :key="keys.id"
                :label="$t(keys.label)"
                :value="keys.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('关键字标签')" prop="keyCode">
            <el-input v-model="form.keyCode" :placeholder="$t('关键字标签')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('关联主表字段')" prop="mainField">
            <!-- <el-input v-model="form.mainField" :placeholder="$t('关联主表字段')" show-word-limit maxlength="32" /> -->
            <el-select
              v-model="form.mainField"
              clearable
              :placeholder="$t('请选择')"
              style="width: 100%"
            >
              <el-option
                v-for="columns in getColumnListByParentId(form.keyId)"
                :key="columns.label"
                :label="$t(columns.label)"
                :value="columns.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('关联的指标字段')" prop="joinField">
            <!-- <el-input v-model="form.joinField" :placeholder="$t('关联的指标字段')" show-word-limit maxlength="32" /> -->
            <el-select
              v-model="form.joinField"
              clearable
              :placeholder="$t('请选择')"
              style="width: 100%"
            >
              <el-option
                v-for="columns in getColumnListByParentId(form.subKeyId)"
                :key="columns.label"
                :label="$t(columns.label)"
                :value="columns.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:word" />
    </div>
  </div>
</template>

<script>
    import { getColumnOfKeyListByType } from '@/api/system/word/keyword/wordOnlineKeyWord'
    import { saveWordOnlineKeyWordSub, getWordOnlineKeyWordSub } from '@/api/system/word/wordOnlineKeyWordSub'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  keyCode: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  subKeyId: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  mainField: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  joinField: [
                      { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                paramsKeyList: []
            }
        },
        created() {
          // 查询所有视图指标
          this.getKeyParamsList()
          // 查询主表所有的字段
        },
        methods: {
            // 初始化数据
            init(flag, data) {
                this.loading = true
                if (flag === 'update') {
                    getWordOnlineKeyWordSub(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset(data.id)
                    this.loading = false
                }
            },
            getKeyParamsList() {
              getColumnOfKeyListByType('all').then((response) => {
                this.paramsKeyList = response.data
              })
            },
            // 表单重置
            reset(keyId) {
                this.form = {
                    id: undefined,
                    keyId: keyId,
                    subKeyId: undefined,
                    keyCode: undefined,
                    mainField: undefined,
                    joinField: undefined,
                    createBy: undefined,
                    updateBy: undefined,
                    createTime: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveWordOnlineKeyWordSub(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          getColumnListByParentId(keyId) {
            return this.paramsKeyList.find(list => list.id === keyId)?.fieldList || []
          }
        }
    }
</script>
